﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels.Activity;
using DormMS.Model.SearchModels.DropGroup;
using Infrastructure;

namespace DormMS.Controllers
{
    public class DropGroupController : BaseController
    {
        protected IDropGroupService dropgroupservice;
        protected IGroupHeadService groupHeadService;
        public DropGroupController(IUserInfoService s, IDropGroupService _dropgroupservice, IGroupHeadService _groupHeadService) : base(s)
        {
            dropgroupservice = _dropgroupservice;
            groupHeadService = _groupHeadService;
        }

        // GET: DropGroup
        public ActionResult DropGroupList()
        {
            return View();
        }

           // GET: DropGroup
        public ActionResult DropGroupEdit()
        {
            return View();
        }


        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DropGroupEdit(int id)
        {
            return Json(new { Result = dropgroupservice.GetVDropGroupInfo(id) });
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDropGroupList(DropGroupSearchModel search)
        {
            Result result = new Result();
            try
            {
                int total = 0;
                var list = dropgroupservice.GetDropGroupList(search, ref total);
                result.data = list;
                result.count = total;
                result.isSuccess = true;
            }
            catch (Exception ex)
            {
                result.msg = ex.Message;
            }
            
            return Json(result);
        }


        [HttpPost]
        public ActionResult DelDropGroup(int id)
        {
            Infrastructure.Result result = new Infrastructure.Result();
            if (dropgroupservice.DelDropGroup(id))
            {
                result.isSuccess = true;
                result.msg = "操作成功";
            }
            else
            {
                result.msg = "操作失败";
            }
            return Json(result);
        }

        /// <summary>
        /// 更新活动状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDropGroupState(int id, int state)
        {
            Infrastructure.Result result = new Infrastructure.Result();
            if (dropgroupservice.UpdateDropGroupState(id, state))
            {
                result.isSuccess = true;
                result.msg = "操作成功";
            }
            else
            {
                result.msg = "操作失败";
            }
            return Json(result);
        }


        /// <summary>
        /// 新增或修改活动
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveDropGroup(DropGroup model)
        {
            Infrastructure.Result result = new Infrastructure.Result();
            if (!dropgroupservice.CheckTitle(model.DropGroupID, model.Title))
            {
                result.msg = "活动名称已存在，请重新输入";
                return Json(result);
            }
            if (model.DropGroupID > 0)
            {
                var entity = dropgroupservice.GetDropGroupInfo(model.DropGroupID);
                if (entity != null)
                {
                    entity.BeginTime = model.BeginTime;
                    entity.CreateTime = DateTime.Now;
                    entity.DropGroupTime = model.DropGroupTime;
                    entity.DropPrice = model.DropPrice;
                    entity.EndTime = model.EndTime;
                    entity.FloorPrice = model.FloorPrice;
                    entity.IsFree = model.IsFree;
                    entity.OriginalPrice = model.OriginalPrice;
                    entity.Remark = model.Remark;
                    entity.SpecificationsID = model.SpecificationsID;
                    entity.StartingPrice = model.StartingPrice;
                    entity.Title = model.Title;
                    entity.CoverImg = model.CoverImg;
                    if (dropgroupservice.EditDropGroup(entity))
                    {
                        result.isSuccess = true;
                    }
                    else
                    {
                        result.msg = "更新失败";
                    }
                }
                else
                {
                    result.msg = "活动不存在";
                }
            }
            else
            {
                model.State = 0;
                model.CreateTime = DateTime.Now;
                model.IsDeleted = false;
                if (dropgroupservice.AddDropGroup(model) > 0)
                {
                    result.isSuccess = true;
                }
                else
                {
                    result.msg = "新增失败";
                }
            }
            return Json(result);
        }
        /// <summary>
        /// 降拼团列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult DropHeadList()
        {
            return View();
        }


        [HttpPost]
        public ActionResult GetDropHeadList(DropGroupSearchModel search)
        {
            int total = 0;
            var list = groupHeadService.GetDropHeadList(search, ref total);
            return Json(new { Total = total, Results = list });
        }


        /// <summary>
        /// 降拼团成员列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult DropMemberList()
        {
            return View();
        }


        [HttpPost]
        public ActionResult GetDropMemberList(DropGroupSearchModel search)
        {
            int total = 0;
            var list = groupHeadService.GetDropMemberList(search, ref total);
            return Json(new { Total = total, Results = list });
        }
    }
}